const db = require("../models");
const logger = require("../utils/utils.logger");
const DAO = require("../dao/DAO");
const Dict = db.dictVal;
const Op = db.Op;

// Create and Save a new Dict
exports.create = (req, res) => {
    const pm = req.body;
    if (!pm.dictId) return res.sendResultAto(null, 605, '字典父节点不能为空')
    if (!pm.key) return res.sendResultAto(null, 605, '字典名不能为空')
    if (!pm.value) return res.sendResultAto(null, 605, '字典值不能为空')
    const dict = {
        dictId: pm.dictId,
        key: pm.key,
        value: pm.value,
    };
    Dict.findOne({ where: { 'key': pm.key, 'value': pm.value } }).then(singleDict => {
        if (singleDict && singleDict.id) return res.sendResultAto(null, 605, '该节点已存在！')
        DAO.create(Dict, dict, data => {
            res.sendResult(data)
        })
    })
};

// Retrieve all Dict from the database.
exports.findAll = (req, res) => {
    const pm = req.body;
    DAO.list(Dict, pm, list => {
        res.sendResult(list)
    })
};

// Find a single Dict with an id
exports.findOne = (req, res) => {
    const pm = req.body;
    DAO.findOne(Dict, pm, data => {
        res.sendResult(data)
    })
};

// Update a Dict by the id in the request
exports.update = (req, res) => {
    const pm = req.body;
    // 请求验证
    DAO.update(Dict, pm, { id: pm.id }, data => {
        res.sendResult(data)
    })

};

// Delete a Dict with the specified id in the request
exports.delete = (req, res) => {
    const pm = req.body;
    // 请求验证
    if (!pm.id) return res.sendResult({ data: '', code: 605, message: "ID不能为空！" })
    DAO.delete(Dict, { id: pm.id }, data => {
        res.sendResult(data)
    })

};



